# Maintainer: Zach Bacon <zachbacon@budgiemedia.rocks>

_realname=vapoursynth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=61
pkgrel=1
pkgdesc="A video processing framework with simplicity in mind (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.vapoursynth.com/"
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cython"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-zimg")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
options=(!strip staticlibs)
source=("vapoursynth-${pkgver}.tar.gz::https://github.com/vapoursynth/vapoursynth/archive/R${pkgver}.tar.gz"
        "0001-wrong-arguments-to-Mem32-constructor.patch"
        "0002-prevent-portable-python.patch"
        "0003-no-registry-setup-py.patch")
sha256sums=('a5d4feeb056679dd1204153250d7c1d38e5a639e995d3c4e3a8e2e8fe1425b75'
            '085331d7c81da760b66864ec34647094384514689e7ea9895e7bd34ed9ce9831'
            'bc75208eb672446a23535455040d0f5a7e49e19e8276773dd9bc30ee87da2e27'
            '7b902630104322c9e78cc937cc050a85888bf40e184c6b0cd34d1872aff3674e')

prepare() {
  cd "${srcdir}/${_realname}-R${pkgver}"
  patch -p1 -i "${srcdir}/0001-wrong-arguments-to-Mem32-constructor.patch"

  # Upstream does not support mingw, see
  # https://github.com/vapoursynth/vapoursynth/pull/841
  patch -p1 -i "${srcdir}/0002-prevent-portable-python.patch"
  patch -p1 -i "${srcdir}/0003-no-registry-setup-py.patch"

  autoreconf -vfi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  ../${_realname}-R${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-python-module=no

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-R${pkgver}"/COPYING.LESSER ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  local _py3ver=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/vapoursynth/*.dll.a
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/python${_py3ver}/site-packages/*.dll.a

  export CFLAGS="-L$(cygpath -wm $pkgdir)/${MINGW_PREFIX}/lib $CFLAGS"
  cd "${srcdir}/${_realname}-R${pkgver}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1
}
